//
//  FinishedCell.m
//  RRQS
//
//  Created by kyjun on 16/3/29.
//
//

#import "FinishedCell.h"
#import "MSubOrder.h"
#import "FinishedSubHeader.h"
#import "FinishedSuCell.h"
#import "FinishedSubFooter.h"

@interface FinishedCell ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong) UITableView* tableView;

@property(nonatomic,strong) UIImageView* iconArrow;

@property(nonatomic,strong) NSString* cellIdentifier;
@property(nonatomic,strong) NSString* headerIdentifier;
@property(nonatomic,strong) NSString* footerIdentifier;


@end

@implementation FinishedCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self){
        self.contentView.backgroundColor = [UIColor colorWithRed:255/255.f green:255/255.f blue:255/255.f alpha:1.0];
        self.cellIdentifier = @"FinshedSuCell";
        self.headerIdentifier = @"FinishedSubHeader";
        self.footerIdentifier = @"FinishedSubFooter";
        [self layoutUI];
        [self layoutConstraints];
    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark =====================================================  user interface layout
-(void)layoutUI{
    [self.contentView addSubview:self.tableView];
    [self.tableView addSubview:self.iconArrow];
}

-(void)layoutConstraints{
    self.tableView.translatesAutoresizingMaskIntoConstraints = NO;
    self.iconArrow.translatesAutoresizingMaskIntoConstraints = NO;
    
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.f]];
    [self.contentView addConstraint:[NSLayoutConstraint constraintWithItem:self.tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0.f]];
    
    [self.iconArrow addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeHeight relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:14.f]];
    [self.iconArrow addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeWidth relatedBy:NSLayoutRelationEqual toItem:nil attribute:NSLayoutAttributeNotAnAttribute multiplier:1.0 constant:7.f]];
    [self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:NSLayoutAttributeCenterY multiplier:1.0 constant:0.f]];
    [self.tableView addConstraint:[NSLayoutConstraint constraintWithItem:self.iconArrow attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.tableView attribute:NSLayoutAttributeRight multiplier:1.0 constant:-30.f]];
}

#pragma mark =====================================================  <UITableViewDataSource>

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
   
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {     
    return self.entity.arrayStore.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    FinishedSuCell *cell = [tableView dequeueReusableCellWithIdentifier:self.cellIdentifier forIndexPath:indexPath];
    cell.goodsName =self.entity.arrayStore[indexPath.row];
    return cell;
}

#pragma mark =====================================================  <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 35.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50.f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50.f;
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    FinishedSubHeader* header =  (FinishedSubHeader*)[tableView dequeueReusableHeaderFooterViewWithIdentifier:self.headerIdentifier];
    header.entity = self.entity;
    return header;
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    FinishedSubFooter* footer = (FinishedSubFooter*)[tableView dequeueReusableHeaderFooterViewWithIdentifier:self.footerIdentifier];
    footer.entity = self.entity;
    return footer;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath{
    return NO;
}


#pragma mark =====================================================  property package
-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate =self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.scrollEnabled = NO;
        _tableView.backgroundColor = [UIColor clearColor];
        [_tableView registerClass:[FinishedSuCell class] forCellReuseIdentifier:self.cellIdentifier];
        [_tableView registerClass:[FinishedSubHeader class] forHeaderFooterViewReuseIdentifier:self.headerIdentifier];
        [_tableView registerClass:[FinishedSubFooter class] forHeaderFooterViewReuseIdentifier:self.footerIdentifier];
    }
    return _tableView;
}

-(UIImageView *)iconArrow{
    if(!_iconArrow){
        _iconArrow = [[UIImageView alloc]init];
        [_iconArrow setImage:[UIImage imageNamed:@"icon-arrow-right"]];
    }
    return _iconArrow;
}

-(void)setArrayOrder:(NSMutableArray *)arrayOrder{
    if(arrayOrder){
        _arrayOrder = arrayOrder;
        [self.tableView reloadData];
    }
}





@end
